<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of annonce
 *
 * @author Gidaff_01
 */
class annonce extends Animus_FrontEnd_Controller {

    //put your code here
    function Annonce() {
        parent::Animus_FrontEnd_Controller();
        $this->load->model('Annonce_m');
        $this->load->helper('date');
    }

    function add_new_annonce() {
        if ($this->input->post('submit')) {
            $formVal = array(
                array(
                    'field' => 'title',
                    'Label' => 'Title',
                    'rules' => 'required|xss_clean'
                ),
                array(
                    'field' => 'description',
                    'Label' => 'Description',
                    'rules' => 'trim|required'
                ),
                array(
                    'field' => 'content',
                    'Label' => 'Content',
                    'rules' => 'required'
                ),
                array(
                    'field' => 'level',
                    'Label' => 'Level',
                    'rules' => 'required'
                )
            );
            $this->form_validation->set_rules($formVal);
            if ($this->form_validation->run() == TRUE) {
                $image_path = './assets/annonce';
                $thumb_path = $image_path . '/thumbs';
                $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 10000,
                    'encrypt_name' => TRUE
                );
                $this->_data['page_title'] = "Uploading...";
                $this->load->library('upload', $config);

                if ($this->upload->do_upload()) {
                    $upload_info = $this->upload->data();
                    $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                    );
                    $this->load->library('image_lib', $config); //load library
                    $this->image_lib->resize(); //do whatever specified in config
                    //delete_pro_file($upload_info['full_path']);

                    $level = $this->input->post('level');
                    $query = $this->Annonce_m->get_day_allow_by_level($level);
                    if ($query->num_rows() > 0) {
                        $d = $query->row();
                    }
                    $dc = new DateClass();
                    $dc->Add("days", (int) $d->day_allow);
                    $string_date = $dc->ToString();
                    $expried_date = human_to_unix($string_date);

                    $data = array('title' => $this->input->post('title'),
                        'description' => $this->input->post('description'),
                        'content' => $this->input->post('content'),
                        'date_posted' => now(),
                        'date_expried' => $expried_date,
                        'posted_by' => $this->session->userdata('user_id'),
                        'level' => $level,
                        'active' => 1,
                        'images' => $upload_info['file_name'],
                        'sub_region' => $this->input->post('region')
                    );
                    $this->Annonce_m->ajouter_annonce($data);
                    redirect('animus_home/list_all_annonces');
                } else {
                    $level = $this->input->post('level');
                    $query = $this->Annonce_m->get_day_allow_by_level($level);
                    if ($query->num_rows() > 0) {
                        $d = $query->row();
                    }
                    $dc = new DateClass();
                    $dc->Add("days", (int) $d->day_allow);
                    $string_date = $dc->ToString();
                    $expried_date = human_to_unix($string_date);
////
                    $data = array('title' => $this->input->post('title'),
                        'description' => $this->input->post('description'),
                        'content' => $this->input->post('content'),
                        'date_posted' => now(),
                        'date_expried' => $expried_date,
                        'posted_by' => $this->session->userdata('user_id'),
                        'sub_region' => $this->input->post('region'),
                        'level' => $level,
                        'active' => 1,
                        'images' => $upload_info['file_name'],
                    );
                    $this->Annonce_m->ajouter_annonce($data);
                    redirect('animus_home/list_all_annonces');
                }
            } else {
                $this->display_view('home/annonce/ajouter_annonce');
            }
        } else {
            $this->display_view('home/annonce/ajouter_annonce');
        }
    }

    function search_annonce($region_id) {
        $per_page = $this->uri->segment(4);
        if ($per_page == "") {
            $per_page = 5;
        }
        $off_set = $this->uri->segment(5);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/annonce/search_annonce/' . '/' . $region_id . '/' . $per_page . '/';
        $config['per_page'] = '5';
        $config['uri_segment'] = 5;
        $config['total_rows'] = $this->Annonce_m->count_search_annonce($region_id);
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['search_annonce'] = $this->Annonce_m->search_annonce($region_id, $config['per_page'], $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $this->Annonce_m->count_search_annonce($region_id);
        $this->_data['page_title'] = "Liste de annonce";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->display_view('home/annonce/search_annonce');
    }

    function detail_annonce($id) {
        $query = $this->Annonce_m->get_annonce_detail($id);
        $this->_data['detail_annonce'] = $query;
        $this->display_view('home/annonce/detail_annonce');
    }

    function search_annonce_adv() {
        $title = $this->input->post('most') != '' ? $this->input->post('most') : '_';
        $region_id = $this->input->post('region') != '' ? $this->input->post('region') : '_';

        $per_page = $this->uri->segment(5);
        if ($per_page == "") {
            $per_page = 5;
        }
        $off_set = $this->uri->segment(6);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['per_page'] = '5';
        $config['uri_segment'] = 6;
        $config['total_rows'] = $this->Annonce_m->count_search_annonce_adv($title, $region_id);
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['selected_region'] = $region_id;
        $this->_data['set_most'] = $title;
        $this->_data['search_annonce'] = $this->Annonce_m->search_annonce_adv($title, $region_id, $config['per_page'], $off_set);
        $config['base_url'] = site_url('annonce/pagination_search') . '/' . $title . '/' . $region_id . '/' . $per_page . '/';
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $this->Annonce_m->count_search_annonce_adv($title, $region_id);
        $this->_data['page_title'] = "Liste de annonce";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;

        $this->display_view('home/annonce/search_annonce');
    }

    function pagination_search($title, $region, $per_page) {
        $this->_data['selected_region'] = $region;
        $this->_data['set_most'] = $title;
        $off_set = $this->uri->segment(6);
        if ($off_set == '')
            $offset = 0;
        $config['per_page'] = $per_page;
        $config['uri_segment'] = 6;
        $config['total_rows'] = $this->Annonce_m->count_search_annonce_adv($title, $region);
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['search_annonce'] = $this->Annonce_m->search_annonce_adv($title, $region, $per_page, $off_set);
        $config['base_url'] = site_url('annonce/pagination_search') . '/' . $title . '/' . $region . '/' . $per_page . '/';
        $this->_data['total'] = $this->Annonce_m->count_search_annonce_adv($title, $region);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();

        $this->display_view('home/annonce/search_annonce');
    }
    function list_annonce(){
            $per_page = $this->uri->segment(3);
            if ($per_page == "") {
                $per_page = 5;
            }
            $off_set = $this->uri->segment(4);
            if ($off_set == "") {
                $off_set = 0;
            }
            $config['base_url'] = base_url() . 'index.php/annonce/list_annonce/' . $per_page . '/';
            $config['per_page'] = '5';
            $config['uri_segment'] = 4;
            $config['total_rows'] = $this->Annonce_m->count_all('animus_annonce');
            if ($off_set > 0 && $off_set == $config['total_rows']) {
                $off_set = $off_set - $per_page;
            }

            $this->_data['annonce'] = $this->Annonce_m->get_annonce($config['per_page'], $off_set);
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->_data['total'] = $this->Annonce_m->count_all('animus_annonce');
            $this->_data['page_title'] = "Liste de annonce";
            $this->_data['per_page'] = $per_page;
            $this->_data['off_set'] = $off_set;
            $this->display_view('home/annonce/list_annonce');

        }

}

?>
